<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\system;

use app\common\dao\system\SystemConfigDao;
use app\common\repositories\BaseRepository;
use app\common\services\CacheService;

/**
 * Class SystemConfigRepository
 *
 * @mixin SystemConfigDao
 */
class SystemConfigRepository extends BaseRepository
{

    public function __construct(SystemConfigDao $dao)
    {
        $this->dao = $dao;
    }


    /**
     * 获取配置
     * @param int $propertyId 企业ID
     * @param string|null $name 配置名
     * @param mixed $default 默认值
     * @return array|mixed|null
     */
    public function getConfig(int $propertyId = 0, string $name = null, $default = null)
    {
        if (empty($name)) {
            return $this->getAllConfig($propertyId);
        }
        $name = explode('.', $name);
        $name[0] = strtolower($name[0]);

        $config = $this->getAllConfig($propertyId);
        foreach ($name as $k => $v) {
            if ($v !== '') {
                if (isset($config[$v])) {
                    $config = $config[$v];
                } else {
                    return $default;
                }
            }
        }

        return $config;
    }


    /**
     * 修改配置
     *
     * @param string $type 类型
     * @param array $params 配置
     * @param int $propertyId 企业ID
     */
    public function modifyConfig(string $type, array $params, int $propertyId = 0)
    {
        $this->dao->whereDelete([
            'property_id' => $propertyId,
            'config_type' => $type
        ]);
        $data = [];
        foreach ($params as $k => $v) {
            $data[] = [
                'key' => $k,
                'value' => serialize($v),
                'config_type' => $type,
                'property_id' => $propertyId
            ];
        }
        $this->dao->insertAll($data);
        $this->clearCache($propertyId);
    }


    /**
     * 获取所有配置
     *
     * @param int $propertyId 企业ID
     * @return array
     */
    private function getAllConfig(int $propertyId = 0): array
    {
        return CacheService::create($propertyId)->remember('get_all_system_config_' . $propertyId, function () use ($propertyId) {
            $data = $this->dao->selectWhere(['property_id' => $propertyId], 'value,key,config_type');
            $config = [];
            foreach ($data as $k => $v) {
                $val = unserialize($v['value']);
                if ($val == 'true') {
                    $val = true;
                } elseif ($val == 'false') {
                    $val = false;
                }
                $config[$v['config_type']][$v['key']] = $val;
            }

            return $config;
        });
    }

    /**
     * 清除缓存
     *
     * @param int $propertyId 企业ID
     * @return void
     */
    private function clearCache(int $propertyId = 0)
    {
        CacheService::create($propertyId)->delete('get_all_system_config_' . $propertyId);
    }
}